//
//  FMDBInfo.m
//  New_Runway
//
//  Created by mack on 2018/3/26.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "FMDBInfo.h"
static FMDBInfo *_DBInfo = nil;

@interface FMDBInfo ()<NSCopying,NSMutableCopying>{
    FMDatabase  *_db;
    
}

@end

@implementation FMDBInfo
+(instancetype)sharedDataBase{
    
    if (_DBInfo == nil) {
        
        _DBInfo = [[FMDBInfo alloc] init];
        
        [_DBInfo initDataBase];
        
    }
    
    return _DBInfo;
    
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    
    if (_DBInfo == nil) {
        
        _DBInfo = [super allocWithZone:zone];
        
    }
    
    return _DBInfo;
    
}

-(id)copy{
    
    return self;
    
}

-(id)mutableCopy{
    
    return self;
    
}

-(id)copyWithZone:(NSZone *)zone{
    
    return self;
    
}

-(id)mutableCopyWithZone:(NSZone *)zone{
    
    return self;
    
}
-(void)initDataBase{
    // 获得Documents目录路径
    
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    // 文件路径
    
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"model.sqlite"];
    
    // 实例化FMDataBase对象
    
    _db = [FMDatabase databaseWithPath:filePath];
    
    [_db open];
    
    // 初始化数据表
    NSString *carInfo = @"CREATE TABLE 'carList' ('id' INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL ,'car_index' VARCHAR(255),'car_title' VARCHAR(255),'car_id' VARCHAR(255),'car_price' VARCHAR(255),'car_url' VARCHAR(255),'car_stock' VARCHAR(255),'car_sale' VARCHAR(255),'car_auth' VARCHAR(255),'car_Licence' VARCHAR(255),'car_mileage' VARCHAR(255),'car_licenceDate' VARCHAR(255),'car_salceDate' VARCHAR(255),'car_order' VARCHAR(255),'car_city' VARCHAR(255),'car_shop' VARCHAR(255),'car_shopId' VARCHAR(255),'car_marketId' VARCHAR(255))";
    
    [_db executeUpdate:carInfo];
    
    
    [_db close];
    
}
- (void)addCarList:(Result *)carModel{
    
    NSMutableArray *data = [self getAllCarLsit];
    
    for (Result *model in data) {
        if ([model.carId isEqualToString:carModel.carId]) {
            [self deleteCarList:model];
        }
    }
    
    [_db open];
    NSNumber *maxID = @(0);
    FMResultSet *res = [_db executeQuery:@"SELECT * FROM carList "];
     //获取数据库中最大的ID
    while ([res next]) {
        if ([maxID integerValue] < [[res stringForColumn:@"car_index"] integerValue]) {
            maxID = @([[res stringForColumn:@"car_index"] integerValue]) ;
        }
    }
    maxID = @([maxID integerValue] + 1);
    
//    NSNumber *stock = [NSNumber numberWithInteger:carModel.carStockState];
//    NSNumber *salce = [NSNumber numberWithInteger:carModel.carSaleState];
//    NSNumber *auth = [NSNumber numberWithInteger:carModel.carAuthState];
//    NSNumber *licenceDate =[NSNumber numberWithInteger:carModel.carLicenceDate];
//    NSNumber *saleTime =[NSNumber numberWithInteger:carModel.carSaleTime];
//    NSNumber *order = [NSNumber numberWithInteger:carModel.carOrderState];
//    NSNumber *licenceStatus = [NSNumber numberWithInteger:carModel.carLicenceState];
//    NSNumber *mileage = [NSNumber numberWithFloat:carModel.carMileage];


    [_db executeUpdate:@"INSERT INTO carList(car_index,car_title,car_id,car_price,car_url,car_stock,car_sale,car_auth,car_Licence,car_mileage,car_licenceDate,car_salceDate,car_order,car_city,car_shop,car_shopId,car_marketId)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",maxID,carModel.carTitle,carModel.carId,@(carModel.carRetailPrice),carModel.carImageUrl,@(carModel.carStockState),@(carModel.carSaleState),@(carModel.carAuthState),@(carModel.carLicenceState),@(carModel.carMileage),@(carModel.carLicenceDate),@(carModel.carSaleTime),@(carModel.carOrderState),carModel.market.city.cityName,carModel.shop.shopName,carModel.shopId,carModel.marketId];
   
    
    [_db close];
//    ,@(carModel.carSaleState),@(carModel.carAuthState),@(carModel.carLicenceDate),@(carModel.carSaleTime),@(carModel.carOrderState)
    
}
-(void)deleteCarList:(Result *)carModel{
    [_db open];
    
    [_db executeUpdate:@"DELETE FROM carList WHERE car_index = ?",carModel.ID];
    
    [_db close];
    
}

-(void)deleteCarListWithID:(NSNumber *)number{
    [_db open];
    
    [_db executeUpdate:@"DELETE FROM carList WHERE car_index = ?",number];
    
    [_db close];
    
}

-(void)updateCarList:(Result *)carModel{
    [_db open];
    
    [_db executeUpdate:@"UPDATE 'carList' SET car_title = ?  WHERE car_index = ?",carModel.carTitle,carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_id = ?  WHERE car_index = ? ",carModel.carId,carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_price = ?  WHERE car_index = ? ",@(carModel.carRetailPrice),carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_url = ?  WHERE car_index = ? ",carModel.carImageUrl,carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_stock = ?  WHERE car_index = ? ",@(carModel.carStockState),carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_sale = ?  WHERE car_index = ? ",@(carModel.carSaleState),carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_auth = ?  WHERE car_index = ? ",@(carModel.carAuthState),carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_Licence = ?  WHERE car_index = ? ",@(carModel.carLicenceState),carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_mileage = ?  WHERE car_index = ? ",@(carModel.carMileage),carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_licenceDate = ?  WHERE car_index = ? ",@(carModel.carLicenceDate),carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_salceDate = ?  WHERE car_index = ? ",@(carModel.carSaleTime),carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_order = ?  WHERE car_index = ? ",@(carModel.carOrderState),carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_city = ?  WHERE car_index = ? ",carModel.market.city.cityName,carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_shop = ?  WHERE car_index = ? ",carModel.shop.shopName,carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_shopId = ?  WHERE car_index = ? ",carModel.shopId,carModel.ID];
    [_db executeUpdate:@"UPDATE 'carList' SET car_marketId = ?  WHERE car_index = ? ",carModel.marketId,carModel.ID];


    [_db close];
    
}
- (NSMutableArray *)getAllCarLsit{
    [_db open];
    
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    
    FMResultSet *res = [_db executeQuery:@"SELECT * FROM carList"];
    
    while ([res next]) {
        Result *carModel = [[Result alloc] init];
        carModel.shop = [[Shop alloc] init];
        carModel.market = [[Market alloc] init];
        carModel.market.city = [[City alloc] init];
        carModel.ID = @([[res stringForColumn:@"car_index"] integerValue]);
        carModel.carTitle = [res stringForColumn:@"car_title"];
        carModel.carId = [res stringForColumn:@"car_id"];
        carModel.carImageUrl = [res stringForColumn:@"car_url"];
        carModel.carRetailPrice = [[res stringForColumn:@"car_price"] floatValue];
        carModel.carStockState = [[res stringForColumn:@"car_stock"] integerValue];
        carModel.carSaleState = [[res stringForColumn:@"car_sale"] integerValue];
        carModel.carAuthState = [[res stringForColumn:@"car_auth"] integerValue];
        carModel.carLicenceState = [[res stringForColumn:@"car_Licence"] integerValue];
        carModel.carMileage = [[res stringForColumn:@"car_mileage"] floatValue];
        carModel.carLicenceDate = [[res stringForColumn:@"car_licenceDate"] integerValue];
        carModel.carSaleTime = [[res stringForColumn:@"car_salceDate"] integerValue];
        carModel.carOrderState = [[res stringForColumn:@"car_order"] integerValue];
        carModel.market.city.cityName = [res stringForColumn:@"car_city"];
        carModel.shop.shopName = [res stringForColumn:@"car_shop"];
        carModel.shopId = [res stringForColumn:@"car_shopId"];
        carModel.marketId = [res stringForColumn:@"car_marketId"];


        [dataArray addObject:carModel];
    }
    
    [_db close];
  
    return dataArray;
    
    
}
- (void)deleteAllCarList{
    [_db open];
    
    NSNumber *maxID = @(0);
    
    FMResultSet *res = [_db executeQuery:@"SELECT * FROM carList "];
    //获取数据库中最大的ID
    while ([res next]) {
        maxID = @([[res stringForColumn:@"car_index"] integerValue] ) ;
        [_db executeUpdate:@"DELETE FROM carList WHERE car_index = ?",maxID];

    }
    [_db close];
    
}

@end
